/*
 * Copyright (c) 2024. The RigelA open source project team and
 * its contributors reserve all rights.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */

use crate::common::{FARPROC, HOOKPROC, LPARAM};
use std::{
    ffi::{c_char, CStr},
    intrinsics::transmute,
};
use windows::core::PCWSTR;

//noinspection SpellCheckingInspection
/**
对FARPROC类型的扩展操作。
*/
pub trait FarProcExt {
    fn to_hook_proc(self) -> HOOKPROC;
}

impl FarProcExt for FARPROC {
    //noinspection SpellCheckingInspection
    /**
    转换到HOOKPROC类型。
    */
    fn to_hook_proc(self) -> HOOKPROC {
        unsafe { transmute(self) }
    }
}

//noinspection SpellCheckingInspection
/**
对LPARAM类型的扩展操作。
*/
pub trait LParamExt {
    /**
    转换到T的引用类型。
    */
    fn to<T>(&self) -> &T;
}

impl LParamExt for LPARAM {
    fn to<T>(&self) -> &T {
        let ptr = self.0 as *const T;
        unsafe { &*ptr }
    }
}

/**
把引用类型转换到bytes。
*/
pub trait ToBytesExt {
    type Input;
    fn to_bytes(&self) -> &[u8] {
        unsafe {
            let ptr = self as *const Self as *const u8;
            std::slice::from_raw_parts(ptr, size_of::<Self::Input>())
        }
    }
}

/**
对字符串的扩展操作。
*/
pub trait StringExt {
    /**
    转换到普通字符串。
    */
    fn to_string(self) -> String;

    /**
    转换到utf16字符串。
    */
    fn to_string_utf16(self) -> String;
}

impl StringExt for *const u8 {
    fn to_string(self) -> String {
        unsafe {
            CStr::from_ptr(self as *const c_char)
                .to_str()
                .unwrap_or("")
                .to_string()
        }
    }

    fn to_string_utf16(self) -> String {
        (self as *const u16).to_string_utf16()
    }
}

impl StringExt for *const u16 {
    fn to_string(self) -> String {
        self.to_string_utf16()
    }

    fn to_string_utf16(self) -> String {
        unsafe { PCWSTR(self).to_hstring().to_string_lossy() }
    }
}

impl StringExt for &[u16] {
    fn to_string(self) -> String {
        self.to_string_utf16()
    }

    fn to_string_utf16(self) -> String {
        let Some(p) = self.iter().position(|x| x == &0) else {
            return String::new();
        };
        String::from_utf16_lossy(&self[..p])
    }
}

/// Vec的扩展操作。
pub trait VecExt<T> {
    fn to_vec(self) -> Vec<T>;
}
